package com.googlecode.jpocketmod;

import java.awt.Frame;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author David Sosby
 */
public class TemplateFileChooser  {
	private JFileChooser chooser;
	private Frame owner = null;
	
	private boolean isSaveDialog = false;
	
	public TemplateFileChooser(Frame owner)  {
		this.owner = owner;
		
		chooser = new JFileChooser();
		chooser.setFileFilter(new TemplateFileFilter());
		chooser.setMultiSelectionEnabled(false);
		
		String templateDirectory = ApplicationProperties.getInstance().getProperty("Settings.StorageFolder");
		if ( templateDirectory != null )  {
			chooser.setCurrentDirectory(new File(templateDirectory));
		}
	}
	
	public void setSaveDialog(boolean isSaveDialog)  { this.isSaveDialog = isSaveDialog;  }
	
	//TODO: Should refactor this to be a Result style class, not a String
	public File getResult()  {
		String buttonText = (isSaveDialog) ? "Save" : "Load";
		int returnValue = chooser.showDialog(owner, buttonText);
		
		if ( returnValue == JFileChooser.APPROVE_OPTION )  {
			return chooser.getSelectedFile();
		} else  {
			return null;
		}
	}
}

class TemplateFileFilter extends FileFilter  {
	public boolean accept(File f)  {
		return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpmt");
	}
	
	public String getDescription()  {  return "JPocketMod Templates";  }
}